package com.wisdudu.frame.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.trello.rxlifecycle.components.support.RxFragment;
import com.wisdudu.frame.R;


/**
 * Created by qyf on 2016/8/11.
 */

public abstract class BaseFragment extends RxFragment {

    public BaseFragmentActivity mActivity;
    private View mView;
    private InputMethodManager inputManager;

    //获取宿主Activity
    protected BaseFragmentActivity getHoldingActivity() {
        return mActivity;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = (BaseFragmentActivity) activity;
    }

    //添加fragment
    public void addFragment(BaseFragment fragment) {
        if (null != fragment) {
            getHoldingActivity().addFragment(this, fragment);
        }
    }


    /**
     * 添加新fragment并移除当前fragment，新fragment返回将不再进入被移除的fragment中
     *
     * @param removeCount 需要往前移除的fragment数量,跨activity直接finish,不要使用此方法
     */
    public void addAndRemoveFragment(int removeCount, BaseFragment fragment) {
        if (null != fragment) {
            if (removeCount < 1) {
                addFragment(fragment);
            } else {
                getHoldingActivity().addAndRemoveFragment(removeCount, fragment);
            }
        }
//        removeFragment();
//        addFragment(fragment);
    }

    //移除fragment
    public void removeFragment() {
        hideKeyboard();
        getHoldingActivity().removeFragment();
    }
//    public void removeFragment(String tag){
//        getHoldingActivity().removeFragment(tag);
//    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View view = inflater.inflate(getLayoutId(), container, false);
//        initView(view, savedInstanceState);
        if (mView != null) {
            ViewGroup parent = (ViewGroup) mView.getParent();
            if (parent != null) {
                parent.removeView(mView);
            }
            return mView;
        }
        mView = initBinding(inflater, container, savedInstanceState);
//        onBindingCreated(mView);
        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        inputManager = (InputMethodManager) getHoldingActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

    }

    public void showMessage(String message) {
        if (getView() != null) {
            Snackbar.make(getView(), message, Snackbar.LENGTH_SHORT).show();
        }
    }

    public void showMessageWithAction(String message, String actionText, View.OnClickListener onClickListener) {
        if (getView() != null) {
            Snackbar.make(getView(), message, Snackbar.LENGTH_SHORT)
                    .setAction(actionText, onClickListener)
                    .show();
        }
    }

    public void showMessageWithAction(View view, String message, String actionText, View.OnClickListener onClickListener) {
        Snackbar.make(view, message, Snackbar.LENGTH_SHORT)
                .setAction(actionText, onClickListener)
                .show();
    }

    /**
     * 仅有返回键及标题情况直接调用即可
     */
    protected void initToolbar(Toolbar toolbar, String title) {
        toolbar.setNavigationIcon(R.drawable.ic_toolbar_back);
        toolbar.setNavigationOnClickListener(view -> {
            initToolbarNavIconClick();
        });
//        TextView toolbarTitle= (TextView) mView.findViewById(R.id.toolbar_title);
        TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        toolbarTitle.setText(title);
        initMenuToolbar(toolbar);
    }

    /**
     * 仅有返回键及标题情况直接调用即可
     */
    protected void initToolbar(Toolbar toolbar, @StringRes int title) {
        toolbar.setNavigationIcon(R.drawable.ic_toolbar_back);
        toolbar.setNavigationOnClickListener(view -> {
            initToolbarNavIconClick();
        });
        TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        toolbarTitle.setText(getResources().getText(title));
        initMenuToolbar(toolbar);
    }

    protected void initToolbarNavIconClick() {
        removeFragment();
    }

    /**
     * 若需要添加右边菜单，重写此方法添加menu及点击事件
     */
    protected void initMenuToolbar(Toolbar toolbar) {

    }

    protected Toolbar getToolbar() {
        if (mView != null) {
            Toolbar toolbar = (Toolbar) mView.findViewById(R.id.toolbar);
            if (toolbar != null) {
                return toolbar;
            } else {
                //检查toolbar是否include进layout中
                throw new RuntimeException("Maybe your layout not include toolbar");
            }
        } else {
            throw new RuntimeException("Should init the root view,please use it in onViewCreated");
        }
    }

    protected abstract View initBinding(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    public void hideKeyboard() {
        if (getActivity() == null) return;
        if (getActivity().getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getActivity().getCurrentFocus() != null)
                inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
//    RequestInfo mRequestInfo = null;
//    public BaseFragmentActivity mFragAct = null;
//
//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        mFragAct = (BaseFragmentActivity) activity;
//    }
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        if (savedInstanceState != null) {
//            mRequestInfo = savedInstanceState.getParcelable("__requestInfo");
//        }
//    }
//
//
//    public Toolbar initToolbar(CharSequence title) {
//        return mFragAct.initToolbar(title);
//    }
//
//    public Toolbar initToolbar(@StringRes int titleRes) {
//        return mFragAct.initToolbar(titleRes);
//    }
//
//    @Override
//    public void onSaveInstanceState(Bundle outState) {
//        if (mRequestInfo != null && outState != null)
//            outState.putParcelable("__requestInfo", mRequestInfo);
//        super.onSaveInstanceState(outState);
//    }
//
//
//    public boolean onBackPressed() {
//        return false;
//    }
//
//    public void onResult(int requestCode, int resultCode, Intent data) {
//
//    }
//
//    protected void setResult(int resultCode, Intent data) {
//        if (mRequestInfo != null) {
//            mRequestInfo = new RequestInfo();
//            mRequestInfo.resultCode = resultCode;
//            mRequestInfo.resultData = data;
//        }
//
//    }
}
